package org.springblade.pm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.pm.entity.PmWeeknote;
import org.springblade.pm.entity.PmWeeknoteEvaluate;
import org.springblade.pm.entity.XmglProject;
import org.springblade.pm.service.IPmWeeknoteService;
import org.springblade.pm.service.IXmglProjectService;
import org.springblade.pm.service.IPmWeeknoteEvaluateService;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 项目周报管理
 */
@RestController
@RequestMapping("pmweeknoteapi")
@AllArgsConstructor
public class PmWeeknoteController {

	private IPmWeeknoteService pmWeeknoteService;
	private IPmWeeknoteEvaluateService pmWeeknoteEvaluateService;
	private IXmglProjectService xmglProjectService;



	/**
	 * 新增修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "新增", notes = "传入PmWeeknote对象")
	public R submitWeeknote(@RequestBody Map<String,Object> map,BladeUser bladeUser){
		String code=map.get("projectID").toString();
		String cycle=map.get("cycle").toString();
		String summary=map.get("summary").toString();
		String plan=map.get("plan").toString();
		Integer status=Integer.valueOf(map.get("zbStatus").toString());
		String problem=map.get("problem").toString();
		PmWeeknote pmWeeknote=new PmWeeknote();
		pmWeeknote.setCycle(cycle);
		pmWeeknote.setPlan(plan);
		pmWeeknote.setProblem(problem);
		pmWeeknote.setStatus(status);
		pmWeeknote.setPlan(plan);
		pmWeeknote.setSummary(summary);
		try {
			List<XmglProject> list=xmglProjectService.getXmlgIdByCode(code);
			pmWeeknote.setCreateUser(bladeUser.getUserId());//获取更新人、修改人
			pmWeeknote.setUpdateUser(bladeUser.getUserId());
			pmWeeknote.setProjectId(list.get(0).getId());
			int proId=list.get(0).getId();
			String weekCount = pmWeeknoteService.selectWeekByProjectId(proId);
			if(weekCount==null){
				pmWeeknote.setWeek("1");
			}else{
				pmWeeknote.setWeek(String.valueOf(Integer.valueOf(weekCount) +1));
			}
			pmWeeknoteService.submit(pmWeeknote);
			return R.status( true );
		}catch (Exception e){
			return R.status(false);
		}
	}

	/**
	 * 修改周报
	 */
	@PostMapping("/updateById")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "修改周报",notes = "传入pmWeeknote对象")
	public R updateWeeknote(PmWeeknote pmWeeknote,String code,Integer zbId,BladeUser bladeUser){
		try {
			List<XmglProject> list=xmglProjectService.getXmlgIdByCode(code);
			pmWeeknote.setCreateUser(bladeUser.getUserId());//获取更新人、修改人
			pmWeeknote.setUpdateUser(bladeUser.getUserId());
			for (XmglProject x:list) {
				int porjectid=x.getId();
				pmWeeknote.setProjectId(porjectid);
			}
			pmWeeknote.setId(zbId);
			pmWeeknoteService.updateById(pmWeeknote);
			return R.status(true);
		}catch (Exception e){
			return R.status(false);
		}

	}
	/**
	 * 新增评价
	 */
	@PostMapping("/insert")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "新增评价",notes = "传入PmWeeknoteEvaluate对象")
	public R insertWeeknoteEvaluate(@RequestBody Map<String,Object> map, BladeUser bladeUser){
		Integer id=Integer.valueOf(map.get("id").toString());
		String summary=map.get("ldfk").toString();
		String opinion=map.get("opinion").toString();
		Integer status=Integer.valueOf(map.get("pjStatus").toString());
		PmWeeknoteEvaluate pmWeeknoteEvaluate=new PmWeeknoteEvaluate();
		pmWeeknoteEvaluate.setWeeknoteId(id);
		pmWeeknoteEvaluate.setOpinion(opinion);
		pmWeeknoteEvaluate.setSummary(summary);
		pmWeeknoteEvaluate.setStatus(status);
		pmWeeknoteEvaluate.setCreateUser(bladeUser.getUserId());
		pmWeeknoteEvaluate.setUpdateUser(bladeUser.getUserId());
		try {
			pmWeeknoteEvaluate.setCreateTime(new Date(System.currentTimeMillis()));
			pmWeeknoteEvaluateService.insert(pmWeeknoteEvaluate);
			return R.status(true);
		}catch (Exception e){
			return R.status(false);
		}
	}
	/**
	 *修改评价
	 */
	@PostMapping("/updateByWeeknoteId")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "修改评价",notes = "传入PmWeeknoteEvaluate对象")
	public R updateWeeknoteEvaluate(@RequestBody Map<String,Object> map,BladeUser bladeUser){
		Integer id=Integer.valueOf(map.get("id").toString());
		String summary=map.get("ldfk").toString();
		String opinion=map.get("opinion").toString();
		Integer status=Integer.valueOf(map.get("pjStatus").toString());
		PmWeeknoteEvaluate pmWeeknoteEvaluate=new PmWeeknoteEvaluate();
		pmWeeknoteEvaluate.setWeeknoteId(id);
		pmWeeknoteEvaluate.setOpinion(opinion);
		pmWeeknoteEvaluate.setSummary(summary);
		pmWeeknoteEvaluate.setStatus(status);
		pmWeeknoteEvaluate.setCreateUser(bladeUser.getUserId());
		pmWeeknoteEvaluate.setUpdateUser(bladeUser.getUserId());
		try {
			pmWeeknoteEvaluate.setCreateTime(new Date(System.currentTimeMillis()));
			pmWeeknoteEvaluateService.updateByWeeknoteId(pmWeeknoteEvaluate);
			return R.status(true);
		}catch (Exception e){
			return R.status(false);
		}
	}

	@GetMapping("/selectWeekList")
	@ApiOperation(value = "查询全部个人周报信息")
	public R<Map<String,Object>> selectWeekList(BladeUser bladeUser,PmWeeknote pmWeeknote,PmWeeknoteEvaluate pmWeeknoteEvaluate,XmglProject xmglProject,Query query){
		Map<String, Object> hashMap = new HashMap<>();
		Map<String, Object> params = new HashMap<>();
		params.put("userId",bladeUser.getUserId());
		params.put("cycle",pmWeeknote.getCycle());
		params.put("code",xmglProject.getCode());
		params.put("leader",xmglProject.getLeader());
		try {
			List<Map<String, Object>> list = pmWeeknoteService.selectWeekList(params);
			if (query.getCurrent()!=null&&!query.getCurrent().equals("")&&query.getSize()!=null&&!query.getSize().equals("")){
				params.put( "limit", getLimitStr(query.getSize(), query.getCurrent()));
				List<Map<String, Object>> mapList = pmWeeknoteService.selectWeekList(params);
				hashMap.put("total",list.size());
				hashMap.put("data",mapList);
				return R.data(hashMap,"查询成功");
			}
			//List<Map<String, Object>> mapList = pmWeeknoteService.selectWeekList(params);
			hashMap.put("total",list.size());
			hashMap.put("data",list);
			return R.data(hashMap,"查询成功");
		} catch (Exception e) {
			return R.data(200, hashMap, "exception:" + e.getMessage());
		}
	}

	@GetMapping("/selectWeekById")
	@ApiOperation(value = "根据id查询周报详情")
	public R<Map<String,Object>> selectWeekById(Integer id,BladeUser bladeUser){
		Map<String, Object> map = null;
		Map<String, Object> params = new HashMap<>();
		params.put("id",id);
		List<String> roleList = Arrays.asList(bladeUser.getRoleId().split(","));
		try {
			if(roleList.contains("9")){
				params.put("role",9);
			}
			map = pmWeeknoteService.selectWeekById(params);
		} catch (Exception e) {
			return R.data(200, map, "exception:" + e.getMessage());
		}
		return R.data(map,"查询成功");
	}

	@GetMapping("/deleteWeekById")
	@ApiOperation(value = "根据id删除周报及评价表信息")
	public R deleteWeekById(Integer id){
		if(pmWeeknoteService.deleteWeekById(id)){
			return R.status(true);
		}
		return R.status(false);
	}

	@GetMapping("/getManager")
	@ApiOperation(value = "项目经理筛选条件下拉框")
	public R<List<Map<String,Object>>> getManager(BladeUser user,String xmCode,String type) {
		List<Map<String, Object>> list =null;
		Map<String, Object> params = new HashMap<>();
		params.put("id",user.getUserId());
		String roles = user.getRoleId();

		List<String> roleArr = null;
		if (Func.isNotBlank(roles)) {
			roleArr = Arrays.asList(roles.split(","));
		}
		try {//type判断是查询全部还是根据权限查询
			if(xmCode==null||"".equals(xmCode)){    //三个场景：1：通过角色权限查项目经理 2：通过xmCode查  3：查全部
				if(type!=null&&!"".equals(type)){
					params.put("type",type);
					list=pmWeeknoteService.getManager(params);
				}else{
					if(roleArr.contains("1")||roleArr.contains("3")||roleArr.contains("7")){
						params.put("grade",1);
						list=pmWeeknoteService.getManager(params);
					}else if (roleArr.contains("8")){
						params.put("grade",2);
						list=pmWeeknoteService.getManager(params);
					}else if(roleArr.contains("9")){
						list=pmWeeknoteService.getManager(params);
					}
				}

			}else{
				params.put("xmCode",xmCode);
				list=pmWeeknoteService.getManager(params);
			}

		} catch (Exception e) {
			e.printStackTrace();
			return R.data(200, list, "exception:" + e.getMessage());
		}
		return R.data(list);
	}



	/**
	 * 根据 页码、页大小 获取分页字符串 " LIMIT 1,10 "
	 */
	public static String getLimitStr(Integer size, Integer page) {
		String str = "";
		try {
			Integer num = (page - 1) * size;
			str = " LIMIT " + num + "," + size + "";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
}
